<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>开始使用 layui</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <style>

        #loginFormDiv {
            width: 600px;
            /*border: solid 1px blue;*/
            margin: 10px auto;
            padding: 20px 60px 20px 20px;
            background-color: transparent;
            border-radius: 50px;
            color: snow;
        }

        .layui-input {
            border-radius: 10px;
        }

        body {
            background-image: url("./images/04 gettyimages-627505342_resized.jpg");
            background-repeat: no-repeat;
            background-size: cover;
            background-attachment: fixed;
        }

        #login{
            font-style: italic;
            font-family: 微软雅黑;
            margin-top: 30px;
            text-align: center;
            font-size: 50px;
            text-align: center;
            color: snow;
        }

        #btn{
            margin: 1px auto;
            text-align: center;
        }

        .layui-form-switch{
            margin-left: 20px;
            margin-top: 0;
        }

        #passwordFreeLoginLabel{
            padding-top: 2px;
            margin-top: 2px;
        }

    </style>
</head>
<body>
<div id="login">
    <label>登&emsp;录</label>
</div>
<div id="loginFormDiv">

    <!-- 你的 HTML 代码 -->
    <form class="layui-form" action="" lay-filter="example">

        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="title" autocomplete="off" placeholder="请输入用户名"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">验证码</label>
            <div class="layui-input-inline">
                <input type="text" name="capchar" autocomplete="off" placeholder="请输入验证码" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <img src="/hutoolCapchar" style="width:85px; height: 38px" id="cap"/>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block" id="btn">
                <button type="submit" class="layui-btn" lay-submit="" lay-filter="loginBtn">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>

                <input type="checkbox" name="autoLogin" lay-skin="switch" lay-text="开启|关闭">

                <label id="passwordFreeLoginLabel">免密登录</label>

            </div>



        </div>

    </form>
</div>


<script src="./layui/layui.js"></script>
<script>
    layui.use(function () {
        var layer = layui.layer
            , form = layui.form
            , $ = layui.$;

        //注册谷歌验证码的单击事件（刷新验证码）
        /*$("#cap").click(function () {
          path = "/captcha?" + new Date().getTime();
          $(this).attr("src", path);
        });*/

        // 处理hutool验证码单击更改图片内容
        $("#cap").click(function () {
            //刷新验证码
            path = "/hutoolCapchar" + "?" + new Date().getTime();
            $(this).attr("src", path);
        });

        $.ajax({
            url: "/user/autoLogin",
            type: "get",
            success: function (result) {
                // 登录成功进行页面跳转
                if (result.code === 1) {
                    window.location = "/plateform.html";
                }
            }
        });

        //监听提交
        form.on('submit(loginBtn)', function (data) {
            /*layer.alert(JSON.stringify(data.field), {
              title: '最终的提交信息'
            });*/
            //发送ajax请求
            //发ajax请求
            $.ajax({
                url: "/user/login",
                data: data.field, //对应的post请求体中的数据格式为username=luffy&password=123456&capchar=LH
                // 如果请求体中要放json，使用如下两行代码
                // contentType:"application/json",
                // data:JSON.stringify(data.field),
                type: "post",
                success: function (result) {
                    console.log(result);
                    // 登录成功进行页面跳转
                    if (result.code === 1) {
                        window.location = "/plateform.html";
                    } else {
                        // 登录失败给出提示信息
                        layer.alert(result.message, {
                            title: '提示信息'
                        });
                    }
                },
                error: function (result) {
                    // 登录失败给出提示信息
                    layer.alert(result.message, {
                        title: '提示信息'
                    });
                }
            });

            //注意：这里要return false; 取消表单默认提交行为（向action对应的url发送请求）
            return false;
        });
        //layer.msg('Hello World');
    });
</script>
</body>
</html>